﻿using System;
using System.Collections;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Gui.Functies.Takenlijst
{
    public partial class Removetaak : UserControl
    {
        private int projectID;
        private string taak;
        private string taakid;
        private string status;
        private Hashtable takenBezig;
        private Hashtable takenKlaar;
        private Hashtable takenNietKlaar;
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn);

        public Removetaak()
        {
            
        }
        //Constructor.
        public Removetaak(int projectnummer, Hashtable bezig, Hashtable klaar, Hashtable nietKlaar)
        {
            projectID = projectnummer;
            takenBezig = bezig;
            takenKlaar = klaar;
            takenNietKlaar = nietKlaar;
            InitializeComponent();
            GetTaken();
        }
        //Haalt de taken op.
        public void GetTaken()
        {
            comboBox1.Items.Clear();
            //taken weergeven die niet klaar zijn
            foreach (string key in takenNietKlaar.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key));
            }

            //taken weergeven die bezig zijn
            foreach (string key in takenBezig.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key));
            }

            //taken weergeven die klaar zijn
            foreach (string key in takenKlaar.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key));
            }
        }
        //Roept de methode aan om de taak te verwijderen.
        private void btnVerwijder_Click(object sender, EventArgs e)
        {
            if (taak == "")
            {
                MessageBox.Show("Er is geen taak geselecteerd");
            }
            else
            {      
                try
                {
                    taak = comboBox1.SelectedItem.ToString();
                    //id moet opgehaald worden aan de hand van de status van de taak
                    MySqlCommand checkStatus = new MySqlCommand("SELECT Status FROM taken WHERE ProjectID='" + projectID + "'AND Taak='" + taak + "'", grabConnection);
                    grabConnection.Open();
                    MySqlDataReader reader = checkStatus.ExecuteReader();

                    while (reader.Read())
                    {
                        status = reader["Status"].ToString();
                    }

                    reader.Dispose();
                    checkStatus.Dispose();
                }
                catch (Exception ex)
                {
                    if (ex is MySqlException || ex is NullReferenceException)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                finally
                {
                    grabConnection.Close();
                }

                DeleteTask();
            }
        }
        //Delete de aangeklikte taak.
        public void DeleteTask()
        {
            if (status == "Bezig")
            {
                taakid = takenBezig[taak].ToString();
            }
            else
            {
                if (status == "Klaar")
                {
                    taakid = takenKlaar[taak].ToString();
                }
                else
                {
                    if (status == "Niet klaar")
                    {
                        taakid = takenNietKlaar[taak].ToString();
                    }
                }
            }

            MySqlCommand deltaak = new MySqlCommand("DELETE FROM taken WHERE TaakID ='"+taakid+"'", grabConnection);
            MySqlCommand deltaaklid = new MySqlCommand("DELETE FROM taak_lid WHERE TaakID ='"+taakid+"'", grabConnection);

            try
            {
                grabConnection.Open();
                deltaak.ExecuteNonQuery();
                deltaaklid.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Controls.Clear();
                this.Controls.Add(new Takenlijst(projectID));
            }
        }
    }
}
